package com.ilop.siteraudio.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author skygge
 * @date 1/26/21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：该类只是一个初始化surfaceview的封装
 */
public class WaveSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

	private final int line_off = 42;//上下边距距离

    public int getLine_off() {
		return line_off;
	}

	public WaveSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		SurfaceHolder holder = getHolder();
		setZOrderOnTop(true);
		holder.setFormat(PixelFormat.TRANSLUCENT);
		holder.addCallback(this);
	}


    /**
     * @author cokus
     * init surfaceview
     */
    public  void initSurfaceView( final SurfaceView sfv){
    	new Thread(){
    		public void run() {
    			 Canvas canvas = sfv.getHolder().lockCanvas(new Rect(0, 0, sfv.getWidth(), sfv.getHeight()));// 关键:获取画布
    	         if(canvas==null){
    	        	 return;
    	         }
    	         canvas.drawARGB(255, 239, 239, 239);
    	        
    	         int height = sfv.getHeight()-line_off;
    	         Paint centerLine =new Paint();
    	         Paint circlePaint = new Paint();
    	         circlePaint.setColor(Color.rgb(246, 131, 126));
    	         circlePaint.setAntiAlias(true);
    	         
    	         canvas.drawCircle(0, line_off/4f, line_off/4f, circlePaint);// 上面小圆
    	         canvas.drawCircle(0, sfv.getHeight()-line_off/4f, line_off/4f, circlePaint);// 下面小圆
    	         canvas.drawLine(0, 0, 0, sfv.getHeight(), circlePaint);//垂直的线
    	         centerLine.setColor(Color.rgb(76, 128, 248));
    	         canvas.drawLine(0, height*0.5f+line_off/2f, sfv.getWidth() ,height*0.5f+line_off/2f, centerLine);//中心线
    	         sfv.getHolder().unlockCanvasAndPost(canvas);// 解锁画布，提交画好的图像
    		};
    	}.start();
    }


	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		initSurfaceView(this);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}
}
